<?php
//微信第三方授权功能实现辅助类
namespace App\Services;

use WechatUserService;
use TestingConfig;
use ParamConfig;


class WxComponentHelper
{
    /**返回加密后的json格式字符串
     * @param $uid
     * @param $tid
     * @param $appid
     * @return string
     *
    {
    "tpl": {
        "touser": "OPENID",
        "template_id": "TEMPLATE_ID",
        "url": "http://tt.qiezilife.com",
        "data": {
            "first": {
                "value": "恭喜你退款成功！",
                "color": "#173177"s
                },
            "reason": {
                "value": "未抢购成功",
                "color": "#173177"
                },
            "refund": {
                "value": "999999元",
                "color": "#173177"
                },
            "remark": {
                "value": "备注：如有疑问，请致电13434联系我们，或回复M来了解详情",
                "color": "#173177"
                }
            }
        },
        "openid": [
            "OPENIDS",
            ]
    }
     */
    public static function getTemplate($uid,$appid,$data){
        //从testing_param_config表中获取消息模板类型
        //$key = $appid."_tpl";
        //$template = ParamConfig::getValue($key);
        $template = '{"tpl":{"touser":"OPENID","template_id":"TEMPLATE_ID","url":"'.$data['redirect'].'","data":{"first":{"value":"'.$data['first'].'","color":"#173177"},"keyword1":{"value":"'.$data['keyword1'].'","color":"#173177"},"keyword2":{"value":"'.$data['keyword2'].'","color":"#173177"},"remark":{"value":"'.$data['remark'].'","color":"#173177"}}},"openid":["OPENIDS"]}';
        //根据uid,appid获取openid
        $userData = WechatUserService::getByUid($uid,$appid);
        if($userData){
            $openid = $userData['openid'];
        }
        //替换消息模板中openid
        $template = str_replace("OPENIDS",$openid,$template);
        //base64_encode加密
        return base64_encode($template);
    }



    /**发送模板
     * @param $package
     * @param $appid
     * @param $tplid
     */
    public static function sendTemplate($package,$appid,$tplid){
        $content = array('package'=>$package);
        $request = TestingConfig::getValue('account.account_center_url');
        //post数据ur
        $request = $request."/Api/Terrace/sendTemplate/appid/".base64_encode($appid)."/tplid/". $tplid;
        curlRequest($request,json_encode($content));
    }





}